//
//  RecommendViewController.m
//  AutoHome
//
//  Created by Flying meat on 9/11/15.
//  Copyright (c) 2015 CaoFeng. All rights reserved.
//

#import "RecommendViewController.h"
#import "AppDelegate.h"
#import "SupperCollectCell.h"
#import "HTTPRequestManager.h"
#import "AFNetworking.h"
#import "Recommend.h"
#import "UIImageView+AFNetworking.h"
#import "MJRefresh.h"
#import "DetailViewController.h"
#import "TabBarViewController.h"
#import "SearchViewController.h"
#import <CoreData/CoreData.h>
@interface RecommendViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,MJRefreshBaseViewDelegate>

{
    UILabel *_label;
    
    NSMutableArray *_newarry;
    NSMutableArray *_dataArray;
    NSMutableArray *_titleArray;
    NSMutableArray *_headimgArray;
    MJRefreshFooterView *_foot;
    MJRefreshHeaderView *_head;
    BOOL _isDragDown;

    NSString *_delete;
   
    
    int _currentPage;
    int _count;//计数器,用来循环滚动
    CGFloat _p;//没网状态下改变label的alpha
    
    int _cctt;//链接的值
    int _nntt;//链接的值
    
    
    int _Creatvideo;//原创视频打标记;
    
    NSString* _jumpid;
    
    
    
    NSManagedObjectContext *_context;
    

}
@property (weak, nonatomic) IBOutlet UIButton *ShareButton;

@property (weak, nonatomic) IBOutlet UIButton *downViewButton;

@property (weak, nonatomic) IBOutlet UIView *TopView;
@property (weak, nonatomic) IBOutlet UIScrollView *TopScrollView;
@property (weak, nonatomic) IBOutlet UIImageView *imgViewWithScroll;
@property (weak, nonatomic) IBOutlet UICollectionView *supperCollectionView;
@end

@implementation RecommendViewController

//初始化
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _currentPage=0;

        _p=1;
        
        _cctt=0;
        _nntt=0;
        _dataArray=[[NSMutableArray alloc]init];
        _titleArray=[[NSMutableArray alloc]init];
        
        _newarry=[NSMutableArray new];
        _headimgArray=[[NSMutableArray alloc]init];
     
        _Creatvideo=1;
        [_titleArray addObjectsFromArray:@[@"最新",@"新闻",@"评测",@"导购",@"行情",@"用车",@"技术",@"文化",@"改装",@"游记",@"原创视频",@"说客",@"快报",@"视频"]];
        _delete=nil;
        

    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden=YES;
    self.supperCollectionView.showsHorizontalScrollIndicator=NO;

    AppDelegate *delegate=[UIApplication sharedApplication].delegate;
    _context=delegate.managedObjectContext;
    
    [self addObserveNetStatus];
    [self setButtonOnTopScroll];
   

}


-(void)loadFromCoredata
{
    
    [_dataArray removeAllObjects];
    NSFetchRequest *request=[NSFetchRequest new];
    
    NSEntityDescription *entity=[NSEntityDescription entityForName:@"Recommend" inManagedObjectContext:_context];
    request.entity=entity;
    
    NSError*error;
    NSArray *resultArray=[_context executeFetchRequest:request error:&error];
    
    [_dataArray addObjectsFromArray:resultArray];
    
    [self.supperCollectionView reloadData];
    
    
    
    
}

-(void)addObserveNetStatus
{
    
    //检测网络状态
    [[AFNetworkReachabilityManager sharedManager]startMonitoring];
    //状态发生改变
    [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status==AFNetworkReachabilityStatusNotReachable) {
           _label=[[UILabel alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height-100, self.view.frame.size.width, 60)];
            _label.text=@"当前网络不可用,请检查网络设置";
            _label.backgroundColor=[UIColor redColor];
            _label.textColor=[UIColor whiteColor];
            _label.textAlignment=NSTextAlignmentCenter;
         
        
             [self.view addSubview:_label];
            
            [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(label) userInfo:nil repeats:YES];
        }else
        {
            [self getInforFromNet];
            
        }
    
    
    }];
    
    
}
-(void)label
{


    if (_p<0) {
        return;
    }
   
   _label.alpha=_p;
    _p=_p-0.01;
    
    
}



//从网络获取数据
-(void)getInforFromNet
{
    if (_cctt!=1000&&_cctt!=1001&&_cctt!=1002&&_cctt!=1003) {
        
    

    [[HTTPRequestManager shareInstance]getRecommendInforByCt:_cctt Nt:_nntt success:^(id responseObject) {
        
  
            [_dataArray removeAllObjects];

        
        
        NSArray *newslist=responseObject[@"newslist"];

        
        //第一页的scorllView的信息
        NSArray *focusimg=responseObject[@"focusimg"];

        NSDictionary *headlineinfo=responseObject[@"headlineinfo"];
        
        NSDictionary *dic=@{@"newslist":newslist,@"headlinefo":headlineinfo,@"focusimg":focusimg};
        [_dataArray addObject:dic];
        [self.supperCollectionView reloadData];
        
        [_foot endRefreshing];
        [_head endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"最新,数据失败error==%@",error);
    }];
    
    }else if (_cctt==1000)
    {
        
        
        [[HTTPRequestManager shareInstance]getRecommendInforByCt:_cctt Nt:_nntt success:^(id responseObject) {
            
            
            [_dataArray removeAllObjects];

            
            NSArray *listArray=responseObject[@"list"];
            NSDictionary *dic=@{@"listArray":listArray};
            [_dataArray addObject:dic];
            [self.supperCollectionView reloadData];
            
            [_foot endRefreshing];
            [_head endRefreshing];
            
        } failure:^(NSError *error) {
            NSLog(@"原创视频数据获取失败,error===%@",error);
        }];
        
        
        
        
    }else if (_cctt==1001)
    {
        
        
        [[HTTPRequestManager shareInstance]getRecommendInforByCt:_cctt Nt:_nntt success:^(id responseObject) {
            
            
            [_dataArray removeAllObjects];

            
            NSArray *listArray=responseObject[@"list"];
            NSDictionary *dic=@{@"listArray":listArray};
            [_dataArray addObject:dic];
            [self.supperCollectionView reloadData];
            
            [_foot endRefreshing];
            [_head endRefreshing];
            
        } failure:^(NSError *error) {
            NSLog(@"原创视频数据获取失败,error===%@",error);
        }];
        
        
        
        
    }else if (_cctt==1002)
    {
        
        
        [[HTTPRequestManager shareInstance]getRecommendInforByCt:_cctt Nt:_nntt success:^(id responseObject) {
            
            
            [_dataArray removeAllObjects];

            
            NSArray *listArray=responseObject[@"list"];
            NSDictionary *dic=@{@"listArray":listArray};
            
            [_dataArray addObject:dic];
            
           [self.supperCollectionView reloadData];
            
            [_foot endRefreshing];
            [_head endRefreshing];
            
        } failure:^(NSError *error) {
            NSLog(@"原创视频数据获取失败,error===%@",error);
        }];
        
        
        
        
    }else if (_cctt==1003)
    {
        
        [[HTTPRequestManager shareInstance]getRecommendInforByCt:_cctt Nt:_nntt success:^(id responseObject) {
            
            
            [_dataArray removeAllObjects];

            
            NSArray *listArray=responseObject[@"list"];
            NSDictionary *dic=@{@"listArray":listArray};
            
            [_dataArray addObject:dic];
            
            [self.supperCollectionView reloadData];
            
            [_foot endRefreshing];
            [_head endRefreshing];
            
        } failure:^(NSError *error) {
            NSLog(@"原创视频数据获取失败,error===%@",error);
        }];
        
        
    }
}





#pragma  mark--设置顶部的button



//设置顶部的button
-(void)setButtonOnTopScroll
{
    self.AboveView=[[UIView alloc]initWithFrame:CGRectMake(0, -(self.view.frame.size.height+200), self.view.frame.size.width, self.view.frame.size.height)];
    self.AboveView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.AboveView];
    
   
    if (_titleArray.count<4) {
        self.imgViewWithScroll.hidden=YES;
    }else
    {
        
        
        self.imgViewWithScroll.hidden=NO;
    }
    
    
    
    //给TopScrollView一个tag值


    self.TopScrollView.tag=201;
    
    
    self.TopScrollView.contentSize=CGSizeMake(_titleArray.count*60, self.TopScrollView.frame.size.height);
    self.TopScrollView.showsHorizontalScrollIndicator=NO;


    for (int i=0; i<_titleArray.count; i++) {
      
    
        
        
        //下划线Button
        
   NSMutableAttributedString *str=[[NSMutableAttributedString alloc]initWithString:_titleArray[i]];
        
        NSRange strRange={0,[str length]};
        [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:strRange];
        
        
        
        
        
         UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font=[UIFont systemFontOfSize:15];
        button.titleLabel.textColor=[UIColor darkGrayColor];

        button.frame=CGRectMake(i*60, 0, 60, 40);
        [button setAttributedTitle:str forState:UIControlStateNormal];

        //给button打上tag值
        button.tag=100+i;
        
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.TopScrollView addSubview:button];
        
        
//        [_dataArray addObject:button];
        
        //创建蓝条调整蓝条的显隐属性
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(i*60+13,42,33, 2)];

        
        label.backgroundColor=[UIColor blueColor];
        label.hidden=YES;
       
        //给label打上tag值;
        label.tag=300+i;
        [self.TopScrollView addSubview:label];
    
        if (i==0) {
            button.selected=YES;
            button.titleLabel.textColor=[UIColor blueColor];
            label.hidden=NO;
        }
        
    }
    
    
}



//点击顶部标题所触发的事件
-(void)click:(UIButton *)button
{
    for (UIButton *button in self.TopScrollView.subviews) {
        //如果是按钮则取反状态
        if ([button isKindOfClass:[UIButton class]]) {
            
            button.selected=NO;
            button.titleLabel.textColor=[UIColor darkGrayColor];
            
            
            UILabel *label=(UILabel*)[self.TopScrollView viewWithTag:button.tag-100+300];
            label.hidden=YES;
            
        }
    }
    button.selected=YES;
    button.titleLabel.textColor=[UIColor blueColor];
    button.backgroundColor=[UIColor clearColor];
    UILabel *label=(UILabel*)[self.TopScrollView viewWithTag:button.tag-100+300];
    label.hidden=NO;
    
    self.supperCollectionView.contentOffset=CGPointMake((button.tag-100)*self.view.frame.size.width,0);
    
    
    
    CGFloat offset=self.supperCollectionView.contentOffset.x;
    int index=offset/self.view.frame.size.width;
    switch (index) {
            //新闻       -c0-nt1
            //评测       -c0-nt3
            //导购       -c0-nt60
            //行情       -c440300-nt2  这里440300是行政编码
            //用车       -c0-nt82
            //技术       -c0-nt102
            //文化       -c0-nt97
            //改装       -c0-nt107
            //游记       -c0-nt100
            
            
        case 0:
            //最新
            _cctt=0;
            _nntt=0;
            [self getInforFromNet];
            break;
        case 1:
            //新闻
            _cctt=0;
            _nntt=1;
            [self getInforFromNet];
            break;
        case 2:
            //评测
            _cctt=0;
            _nntt=3;
            [self getInforFromNet];
            break;
        case 3:
            //导购
            _cctt=0;
            _nntt=60;
            [self getInforFromNet];
            break;
        case 4:
            //行情
            _cctt=440300;
            _nntt=2;
            [self getInforFromNet];
            break;
        case 5:
            //用车
            _cctt=0;
            _nntt=82;
            [self getInforFromNet];
            break;
        case 6:
            //技术
            _cctt=0;
            _nntt=102;
            [self getInforFromNet];
            break;
        case 7:
            //文化
            _cctt=0;
            _nntt=97;
            [self getInforFromNet];
            break;
        case 8:
            //改装
            _cctt=0;
            _nntt=107;
            [self getInforFromNet];
            break;
        case 9:
            //游记
            _cctt=0;
            _nntt=100;
            [self getInforFromNet];
            break;
        case 10:
            //原创视频


            _cctt=1000;

            [self getInforFromNet];
            break;
        case 11:
            //说课
            
            
            _cctt=1001;
            
            [self getInforFromNet];
            break;
        case 12:
            //快报
            
            
            _cctt=1002;
            
            [self getInforFromNet];
            break;
        case 13:
            //视频
            
            _nntt=0;
            _cctt=1003;
            
            [self getInforFromNet];
            break;
        default:
            break;
    }
    
    
    
    
    
}




//滑动的下标从0开始到13
-(void)updateTopbuttonWithIndex:(int)index
{
    
}




#pragma  mark -顶部向下箭头点击掉下新的View




#define originX 10 //x起点;
#define originY 5  //y起点;
#define with (self.view.frame.size.width/5) //宽度
#define heightt 25  //高度
#define space 10  //间隙


//顶部向下箭头点击调下新的View
- (IBAction)cilkDownView:(id)sender {
    
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:1.0];


    self.AboveView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-100);
    
    
    

    
    self.TopScrollView.hidden=YES;
    self.downViewButton.hidden=YES;
    self.ShareButton.hidden=YES;

    self.supperCollectionView.userInteractionEnabled=NO;

    
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(self.AboveView.frame.size.width/2-30, self.AboveView.frame.size.height-30, 60, 30)];
    
    UIView *downView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.AboveView.frame.size.width, 64)];
    
    downView.backgroundColor=[UIColor colorWithRed:179 green:179 blue:186 alpha:1];
    
    
    //下划线button
    NSMutableAttributedString *str=[[NSMutableAttributedString alloc]initWithString:@"这里是全部频道"];
    
    NSRange strRange={0,[str length]};
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:strRange];
    
    UIButton *allbt=[UIButton buttonWithType:UIButtonTypeCustom];
    allbt.frame=CGRectMake(10,20,120, 44);
    allbt.titleLabel.font=[UIFont systemFontOfSize:17];
    allbt.titleLabel.textColor=[UIColor blueColor];
    
    [allbt setAttributedTitle:str forState:UIControlStateNormal];
    
    [downView addSubview:allbt];

    UILabel *longlabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 1)];
    longlabel.backgroundColor=[UIColor lightGrayColor];
    [downView addSubview:longlabel];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 62, 120, 2)];
    label.backgroundColor=[UIColor blueColor];
    [downView addSubview:label];
    
//    self.AboveView.backgroundColor=[UIColor colorWithRed:179 green:179 blue:186 alpha:0.9995];
    
    [self.AboveView addSubview:downView];
    
    
    
    NSMutableAttributedString *str2=[[NSMutableAttributedString alloc]initWithString:@"成功!✌️"];
    
    NSRange strRange2={0,[str2 length]};
    [str2 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:strRange2];

    
    UIButton *donebutton=[UIButton buttonWithType:UIButtonTypeCustom];
    donebutton.frame=CGRectMake(220, 20, 90,44);
    donebutton.titleLabel.font=[UIFont systemFontOfSize:20];
    
    //这里可以起一个定时器达到循环变色的效果.
    donebutton.titleLabel.textColor=[UIColor colorWithRed:arc4random()%265/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    
    
    
    
    [donebutton addTarget:self action:@selector(chooseComplete) forControlEvents:UIControlEventTouchUpInside];
    
    
    [donebutton setAttributedTitle:str2 forState:UIControlStateNormal];
    
    [downView addSubview:donebutton];
    
    
    
    
    
    
    
    
    
    
    
    
    
    [button addTarget:self action:@selector(backbefore) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"ah_groupView_back.png"] forState:UIControlStateNormal];
    
    [ self.AboveView addSubview:button];
    
    
    
    self.newscrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width,374)];
    self.newscrollview.backgroundColor=[UIColor colorWithRed:179 green:179 blue:186 alpha:0.9995];
    self.newscrollview.contentSize=CGSizeMake(0, self.AboveView.frame.size.height);
    self.newscrollview.showsVerticalScrollIndicator=YES;
    self.newscrollview.tag=666;
    self.newscrollview.delegate=self;
    [self.AboveView addSubview:self.newscrollview];
    

    
    
    UILabel *bigGrayLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 40)];
    UILabel *smallGrayLabel=[[UILabel alloc]initWithFrame:CGRectMake(120, 0, 150, 40)];

    
    
    smallGrayLabel.text=@"长按拖动排序/点击删除";
    smallGrayLabel.textColor=[UIColor lightGrayColor];
    smallGrayLabel.font=[UIFont systemFontOfSize:13];
    
    
    bigGrayLabel.text=@"已选频道😊";
    bigGrayLabel.textColor=[UIColor darkGrayColor];
    bigGrayLabel.font=[UIFont systemFontOfSize:16];
    
    
    [self.newscrollview addSubview:bigGrayLabel];
    [self.newscrollview addSubview:smallGrayLabel];

   self.buttonView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.newscrollview.frame.size.height-50)];
    
    
    self.buttonView.backgroundColor=[UIColor clearColor];
    [self.newscrollview addSubview:self.buttonView];
    
    
    
    
  
    [self refreshdonwView];
    
        


    
    
    [UIView commitAnimations];
  
    
    
}



-(void)refreshdonwView
{
    //每次布局之前,将所有的视图清空
    for (UIView *view in self.buttonView.subviews) {
        [view removeFromSuperview];
    }
    

    
    
    
    //根据选择的数组内容来布局
    for (int i=0;i<_titleArray.count; i++) {
        //下划线button
        NSMutableAttributedString *str=[[NSMutableAttributedString alloc]initWithString:_titleArray[i]];
        
        NSRange strRange={0,[str length]};
        [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:strRange];
        
        UIButton *buttonb=[UIButton buttonWithType:UIButtonTypeCustom];
        buttonb.frame=CGRectMake(10,20,120, 44);
        buttonb.titleLabel.font=[UIFont systemFontOfSize:17];
        buttonb.titleLabel.textColor=[UIColor blueColor];
        buttonb.titleLabel.adjustsFontSizeToFitWidth=YES;
        [buttonb setAttributedTitle:str forState:UIControlStateNormal];
        
        buttonb.frame=CGRectMake(originX+(i%4)*(with+space), originY +(i/4)*(heightt+space), with, heightt);
        
        //添加判断条件
        
        //添加删除事件
        [buttonb addTarget:self action:@selector(deleteAcion:) forControlEvents:UIControlEventTouchUpInside];
        
        
        buttonb.tag=200+i;
        
        
       
        
        
        
        [self.buttonView addSubview:buttonb];
    }
    
    
    UILabel *secondbigGrayLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,originY +(_titleArray.count/4)*(heightt+space)+30, 150, 40)];
    UILabel *secondsmallGrayLabel=[[UILabel alloc]initWithFrame:CGRectMake(170, originY +(_titleArray.count/4)*(heightt+space)+30, 150, 40)];
    self.secondbuttView=[[UIView alloc]initWithFrame:CGRectMake(0,originY +(_titleArray.count/4)*(heightt+space)+70, self.view.frame.size.width, 240)];
    
    
    self.secondbuttView.backgroundColor=[UIColor clearColor];
    
    
    secondsmallGrayLabel.text=@"点击添加➕";
    secondsmallGrayLabel.textColor=[UIColor lightGrayColor];
    secondsmallGrayLabel.font=[UIFont systemFontOfSize:13];
    
    
    secondbigGrayLabel.text=@"待选🚥🚗☁️🏃🅿️";
    secondbigGrayLabel.textColor=[UIColor darkGrayColor];
    secondbigGrayLabel.font=[UIFont systemFontOfSize:16];
    
    
    [self.buttonView addSubview:secondbigGrayLabel];
    [self.buttonView addSubview:secondsmallGrayLabel];
    [self.buttonView addSubview:self.secondbuttView];
    
    
    
    //将删除的数据添加到新的数组
   
    
    if (_delete==nil) {
        
        if (_newarry.count!=0) {
            
            for (UIView *view in self.secondbuttView.subviews) {
                [view removeFromSuperview];
            }
            for (int i=0;i<_newarry.count; i++) {
                //下划线button
                NSMutableAttributedString *str=[[NSMutableAttributedString alloc]initWithString:_newarry[i]];
                
                NSRange strRange={0,[str length]};
                [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:strRange];
                
                UIButton *buttonc=[UIButton buttonWithType:UIButtonTypeCustom];
                buttonc.frame=CGRectMake(10,20,120, 44);
                buttonc.titleLabel.font=[UIFont systemFontOfSize:17];
                buttonc.titleLabel.textColor=[UIColor blueColor];
                buttonc.titleLabel.adjustsFontSizeToFitWidth=YES;
                
                buttonc.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.5];
                [buttonc setAttributedTitle:str forState:UIControlStateNormal];
                
                buttonc.frame=CGRectMake(originX+(i%4)*(with+space), originY +(i/4)*(heightt+space), with, heightt);
                
                //添加判断条件
                
                //添加删除事件
                [buttonc addTarget:self action:@selector(addbuttonTo:) forControlEvents:UIControlEventTouchUpInside];
                
                
                buttonc.tag=700+i;
                
                
                
                
                
                
                [self.secondbuttView addSubview:buttonc];
            }

            
            
        }
        return;
    }else{
    [_newarry addObject:_delete];
   
    
    for (UIView *view in self.secondbuttView.subviews) {
        [view removeFromSuperview];
    }
    for (int i=0;i<_newarry.count; i++) {
        //下划线button
        NSMutableAttributedString *str=[[NSMutableAttributedString alloc]initWithString:_newarry[i]];
        
        NSRange strRange={0,[str length]};
        [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:strRange];
        
        UIButton *buttonc=[UIButton buttonWithType:UIButtonTypeCustom];
        buttonc.frame=CGRectMake(10,20,120, 44);
        buttonc.titleLabel.font=[UIFont systemFontOfSize:17];
        buttonc.titleLabel.textColor=[UIColor blueColor];
        buttonc.titleLabel.adjustsFontSizeToFitWidth=YES;
       buttonc.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.5];
        [buttonc setAttributedTitle:str forState:UIControlStateNormal];
        
        buttonc.frame=CGRectMake(originX+(i%4)*(with+space), originY +(i/4)*(heightt+space), with, heightt);
        
        //添加判断条件
        
        //添加删除事件
        [buttonc addTarget:self action:@selector(addbuttonTo:) forControlEvents:UIControlEventTouchUpInside];
        
        
        buttonc.tag=700+i;
        
        
        
        
        
        
        [self.secondbuttView addSubview:buttonc];
    }
    
    }
    
    
    
    
    
}



-(void)addbuttonTo:(UIButton *)addButton
{
    [_titleArray addObject:_newarry[addButton.tag-700]];


    [_newarry removeObjectAtIndex:addButton.tag-700];
    _delete=nil;
    [self refreshdonwView];
    
}


#pragma  mark---删除button事件的处理
-(void)deleteAcion:(UIButton*)button
{
    
    
    
    if ([_titleArray[button.tag-200] isEqualToString:@"最新"]) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"温馨提示💋" message:@"不能删除最新" delegate:nil cancelButtonTitle:@"ok 我懂了✌️" otherButtonTitles:nil, nil];
    
        [alert show];
        return;
    }else{
        _delete=_titleArray[button.tag-200];
        
        
    [_titleArray removeObjectAtIndex:button.tag-200];
    
    
    [self refreshdonwView];
    
    }
}


#pragma -mark --------点击成功之后
-(void)chooseComplete
{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:1.0];
    
    
    self.AboveView.frame=CGRectMake(0,-(self.view.frame.size.height+200), self.view.frame.size.width, self.view.frame.size.height);
    
    
    
    self.TopScrollView.hidden=NO;
    self.downViewButton.hidden=NO;
    self.ShareButton.hidden=NO;

    self.supperCollectionView.userInteractionEnabled=YES;
    
    for (UIButton *bt in self.TopScrollView.subviews) {
        [bt removeFromSuperview];
    }
    
    
    
    [UIView commitAnimations];

    
    
    [self viewDidLoad];
    
    
}


-(void)dealloc
{
    //使用MJ一定要释放,否则胡崩溃
    
    
    [_foot free];
}
#pragma mark---点击上箭头恢复原样
-(void)backbefore
{
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:1.0];
    
    
    self.AboveView.frame=CGRectMake(0,-(self.view.frame.size.height+200), self.view.frame.size.width, self.view.frame.size.height);
    
    
    
    self.TopScrollView.hidden=NO;
    self.downViewButton.hidden=NO;
    self.ShareButton.hidden=NO;

    self.supperCollectionView.userInteractionEnabled=YES;
    
    for (UIButton *bt in self.TopScrollView.subviews) {
        [bt removeFromSuperview];
    }
    
    
    
    [UIView commitAnimations];
    
    
    
    [self viewDidLoad];

    
}


#pragma mark----搜索按钮
//顶部搜索按钮,点击退出搜索界面
- (IBAction)searchButton:(id)sender {
    
    
    SearchViewController *searchVC=[self.storyboard instantiateViewControllerWithIdentifier:@"SearchViewController"];
   
    [self presentViewController:searchVC animated:NO completion:nil];
    
    
    
    
    
}




//滑动顶部标题,当最后一项滑出时,阴影栏消失,当最后一项滑入时又出现
#pragma mark -UIScrollViewDelegate阴影栏的设置
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_titleArray.count<4) {
        self.imgViewWithScroll.hidden=YES;
    }else if(self.TopScrollView.contentOffset.x>_titleArray.count*60-240) {
        self.imgViewWithScroll.hidden=YES;
    }else{
        self.imgViewWithScroll.hidden=NO;
    }

    
    
}

#pragma mark -UICollectionViewDataSource

// 有多少个标题就返回多少个CollectionVeiwCell
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _titleArray.count;
}




//CollectionViewCell复用
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

        
   
   SupperCollectCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"SupperCollectCell" forIndexPath:indexPath];
    
    cell.scrollview=NULL;
    cell.imageVieww=NULL;
    cell.page=NULL;
    
    
    
    cell.callBack=^(NSString* iii){
        
        DetailViewController *detavc=[self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
        detavc.EveryID=iii;
        
        [self presentViewController:detavc animated:YES completion:nil];
        NSLog(@"1234567");
    };
   

    
    
    _foot = [[MJRefreshFooterView alloc]init];
    _foot.scrollView = cell.myTableView ;
    _foot.delegate =self;
    
    _head = [[MJRefreshHeaderView alloc]init];
    _head.scrollView = cell.myTableView;
    _head.delegate =self;

    

    
    
    
      cell.allArray=_dataArray;



    [cell.myTableView reloadData];
    
    //复用时,让littleCollectionView滚动到顶部
    [cell.myTableView scrollToRowAtIndexPath:0 atScrollPosition:UITableViewScrollPositionNone animated:NO];
    return cell;
    
  

    
}




#pragma mark -MJRefreshBaseViewDelegate
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (refreshView == _head) {
        _isDragDown = YES;

    }else
    {
        _isDragDown = NO;

    }
    //重新请求数据
   
    
    [self getInforFromNet];
    
    }





//让cell能够充满屏幕
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return collectionView.frame.size;
    
    
}










-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    
        if (scrollView.tag==201) {
        NSLog(@"4444");
        
    }else{

        
       
        CGFloat offset=scrollView.contentOffset.x;
        if (offset>self.view.frame.size.width*3) {
            
        
        int index=(offset-self.view.frame.size.width*3)/self.view.frame.size.width;

       [self.TopScrollView setContentOffset:CGPointMake(60*index, 0)animated:YES];

            UIButton *currentbutton=(UIButton*)[self.TopScrollView viewWithTag:100+index+3];
            
            for (UIButton *button in self.TopScrollView.subviews) {
                //如果是按钮则取反状态
                if ([button isKindOfClass:[UIButton class]]) {
                    
                    button.selected=NO;
                    button.titleLabel.textColor=[UIColor darkGrayColor];
                    
                    
                    UILabel *label=(UILabel*)[self.TopScrollView viewWithTag:button.tag-100+300];
                    label.hidden=YES;
                    
                }
            }
            currentbutton.selected=YES;
            currentbutton.titleLabel.textColor=[UIColor blueColor];
            currentbutton.backgroundColor=[UIColor clearColor];
            UILabel *label=(UILabel*)[self.TopScrollView viewWithTag:currentbutton.tag-100+300];
            label.hidden=NO;

            
            
            
    
            
            
            //行情
            switch (index+3) {
                case 4:
                    _cctt=440300;
                    _nntt=2;
                    [self getInforFromNet];
                    

                    break;
                    
                    //用车
                case 5:
                    _cctt=0;
                    _nntt=82;
                    [self getInforFromNet];
                    
                    
                    break;
                    //技术
                case 6:
                    _cctt=0;
                    _nntt=102;
                    [self getInforFromNet];
                    
                    
                    break;
                    //文化
                case 7:
                    _cctt=0;
                    _nntt=97;
                    [self getInforFromNet];
                    
                    
                    break;
                    //改装
                case 8:
                    _cctt=0;
                    _nntt=107;
                    [self getInforFromNet];
                    
                    
                    break;
                    //游记
                case 9:
                    _cctt=0;
                    _nntt=100;
                    [self getInforFromNet];
                    
                    break;
                    
                    
                    //原创视频
                case 10:


                    _cctt=1000;
                    [self getInforFromNet];
                    
                    
                    break;
                    //说课
                case 11:
                    _cctt=1001;

                    [self getInforFromNet];
                    
                    
                    break;
                    //快报
                case 12:
                    _cctt=1002;
                    [self getInforFromNet];
                    break;
                case 13:
                    _nntt=0;
                    _cctt=1003;
                    [self getInforFromNet];
                    break;

                    
                default:
                    break;
            }
            
            
        }else
        {

            
           

            int index=(offset/self.view.frame.size.width);
        
            
            UIButton *currentbutton=(UIButton*)[self.TopScrollView viewWithTag:100+index];

            
            
            
            for (UIButton *button in self.TopScrollView.subviews) {
                //如果是按钮则取反状态
                if ([button isKindOfClass:[UIButton class]]) {
                    
                    button.selected=NO;
                    button.titleLabel.textColor=[UIColor darkGrayColor];
                    
                    
                    UILabel *label=(UILabel*)[self.TopScrollView viewWithTag:button.tag-100+300];
                    label.hidden=YES;
                    
                }
            }
           currentbutton.selected=YES;
           currentbutton.titleLabel.textColor=[UIColor blueColor];
            currentbutton.backgroundColor=[UIColor clearColor];
            UILabel *label=(UILabel*)[self.TopScrollView viewWithTag:currentbutton.tag-100+300];
            label.hidden=NO;
        
        
        
        
        
            [self.TopScrollView setContentOffset:CGPointMake(0, 0)animated:YES];
            
            if (index==0) {
                _cctt=0;
                _nntt=0;
                
                [self getInforFromNet];
            }else if (index==1) {
                _cctt=0;
                _nntt=1;
                
                [self getInforFromNet];
                
            }else if (index==2){
                
                _cctt=0;
                _nntt=3;
                [self getInforFromNet];
            }else if(index==3)
            {
                _cctt=0;
                _nntt=60;
                [self getInforFromNet];
            }
            
          
    
            
            
        }
//        [self updateTopbuttonWithIndex:index];
       
    }

    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    NSLog(@"我是collectionviewcell");

     

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    if ([segue.identifier isEqualToString:@"DetailPush"]) {
//        DetailViewController *detail=segue.destinationViewController;
//        detail.EveryID=_jumpid;
//        
//    }
//    
//    
//    
//    
//}


@end
